import ccclass = cc._decorator.ccclass;
import property = cc._decorator.property;
import InviteItem, {InviteItemState} from "./InviteItem";
import {apiUtils} from "../web/api/ApiUtils";
import {wxUtils} from "../wx/wxUtils";
import {model} from "../web/model/Model";
import {StatEvent, Statistic} from "../../statistic/statistic";

@ccclass
export default class InviteBoard extends cc.Component {
  @property([cc.Node])
  inviteItemNodes: Array<cc.Node> = []

  onLoad() {
    this.inviteItemNodes.forEach((value, index) => {
      value.getComponent(InviteItem).id = index + 1
    })
    apiUtils.reqInviteState().then(value => {
      value.inviteState.forEach(value1 => {
        const node = this.inviteItemNodes[value1.index - 1]
        node.getComponent(InviteItem).state = value1.state
      })
    });
  }

  async onNodeTouch(e, data: any) {
    const item = e.target.getComponent(InviteItem)
    if (item.state === InviteItemState.RECEIVABLE) {
      const code = await apiUtils.getInviteReward(parseInt(data))
      if (code === 0) {
        item.state = -1
        // WordsUtils.success('领取成功')
      }
    }
  }

  showInvite() {
    Statistic.emit(StatEvent.INVITE_CLICK)
    wxUtils.shareToFriends()
  }

  onClose() {
    this.node.destroy()
    wxUtils.showMenuAd()
  }
}